

#include "Losu_Std.h"
#include<iostream>
#include<string>
#include<vector>
using namespace std;
extern "C"{
extern LNI_return rt;
extern string rtstr;
static vector<int> pool;
static vector<FILE*> ptrpool;
LNI_return Els_Os_getenv(LNI_argv a){
    rt.type = ELS_TYPE_STRING;
    if(!(rt.str = getenv(a.str[3]))) rt.type == ELS_TYPE_NULL;
    return rt;
}
LNI_return Els_Os_system(LNI_argv a){
    rt.type = ELS_TYPE_NUMBER;
    rt.num = system(a.str[3]);
    return rt;
}

LNI_return Els_Os_Popen_freenum(LNI_argv a){
    rt.type=ELS_TYPE_NUMBER;
    int i;
    for(i=0;i<pool.size();i++){
        rt.num = i;
        if(!pool[i]){ 
            pool[i]=1;    
            return rt;
        }
    }
    pool.push_back(1);
    ptrpool.push_back((FILE*)(0));
    rt.num = pool.size()-1;
    return rt;
}
LNI_return Els_Os_Popen_popen(LNI_argv a){
    rt.type = ELS_TYPE_NULL;
    ptrpool[a.num[3]] = popen(a.str[4],a.str[5]);
    return rt; 
}
LNI_return Els_Os_Popen_read(LNI_argv a){
    char s[(int)(a.num[4])];
    fgets(s,(int)(a.num[4])+1,ptrpool[a.num[3]]);rtstr=s;
    rt.type = ELS_TYPE_STRING;
    rt.str = (char*)rtstr.c_str();
    return rt;
}
LNI_return Els_Os_Popen_write(LNI_argv a){
    rt.type = ELS_TYPE_NULL;
    fwrite(a.str[4],sizeof(a.str[4]),1,ptrpool[a.num[3]]);
    return rt;
}
LNI_return Els_Os_Popen_close(LNI_argv a){
    rt.type = ELS_TYPE_NULL;
    pool[a.num[3]]=0;
    pclose(ptrpool[a.num[3]]);
    return rt;
}
}
